using LanqiQAwebapi.Dto;
using LanqiQAwebapi.Interface;

using LanqiQAwebapi.Models;
using LanqiQAwebapi.ToolsHader;

namespace LanqiQAwebapi.Todo

{
    public class UserServer : IUserServer
    {

        private readonly LanQiQaDBContext _LanQiQaDBContext;

        private readonly ICacheService _cacheservice;
        private readonly Ijwt _ijwt;

        public UserServer(LanQiQaDBContext lanQiQaDBContext, Ijwt ijwt, ICacheService cacheservice)

        {
            _LanQiQaDBContext = lanQiQaDBContext;
            _ijwt = ijwt;
            _cacheservice = cacheservice;

        }
        public bool Userhave(User user) { return true; }
        public bool Edituser(User user) { return true; }
        public User GetUser(User user) { var data = _LanQiQaDBContext.Users.FirstOrDefault(); return data; }

        public User GetUserName(string id) { var user = _LanQiQaDBContext.Users.FirstOrDefault(); return user; }

        public List<User> GetUsers()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 用户登录颁布长期token,token
        /// </summary>
        /// <param name="userLoginByUser"></param>
        /// <returns></returns>
        public string UserLoginByUser(UserLoginByUser userLoginByUser)
        {
            var HavedUser = _LanQiQaDBContext.Users.Where(o => o.UserName == userLoginByUser.Username && o.Password == userLoginByUser.UserPwd).Select(c => new { c.UserId, c.UserName }).FirstOrDefault();


            if (HavedUser == null)
            {
                return "还没有注册";
            }
            else if (_cacheservice.CheckTokenIsBlacke(HavedUser.UserId.ToString(), userLoginByUser.Token ?? ""))
            {
                return _ijwt.AccessToken(HavedUser.UserId.ToString(), HavedUser.UserName.ToString(), "");
            }
            else
            {
                return "过期或者黑名单";
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="userLoginByphone"></param>
        /// <returns></returns>
        public string UserLoginByphone(LoginByPhone loginByPhone)
        {
            var HavedUser = _LanQiQaDBContext.Users.Where(o => o.Phone == loginByPhone.Phone).Select(c => new { c.Phone, c.UserId, c.UserName }).FirstOrDefault();
            if (HavedUser == null) return "你已成功注册请登录";
            var Code = CheckMessage(loginByPhone.Phone, loginByPhone.PhoneCode);
            if (Code == 1)
            {

                if (_cacheservice.CheckTokenIsBlacke(HavedUser.UserId.ToString(), loginByPhone.Token))
                {
                    return _ijwt.AccessToken(HavedUser.UserId.ToString(), HavedUser.UserName.ToString(), "");
                }
                else
                {
                    return "请重新登录";
                }

            }
            else if (Code == 2) { return "验证码已经过期"; }
            else { return "还没有发送验证码"; }
        }
        public string SendPhoneMMessageCode(UserLoginByphone userLoginByphone)
        {

            var Messagecode = AddMessage.SendMessage(userLoginByphone.Phone);
            if (_cacheservice.AddSendMessage(userLoginByphone.Phone, Messagecode.ToString()))
            {
                return Messagecode.ToString();
            }

            return "发送次数过多请次日再试！";


        }
        private int CheckMessage(string Phone, string MessageCode)
        {


            var count = _cacheservice.PGetSendMessageCount(Phone);
            if (count > 0)//有验证码
            {
                var redisPhone = _cacheservice.GetPhoneMessage(Phone, count);
                if (redisPhone.Length > 0 && redisPhone == MessageCode)//验证码没过期，且验证码相同
                {
                    return 1;
                }
                else return 2;
            }

            return 0;

        }


    }
}
